﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreLib;
using CoreLib.BusinessLogic;
using CoreLib.DAL;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Drawing.Layout;
using PdfSharp.Pdf;

namespace RKMSikraKulingram.PDFUtil
{
    internal class PersonDetailsPostToPDF
    {
        public static void PersonDetailsPostSaveToPDF()
        {
            string text = "";

            PersonDetailsPost[] personDetailsPostList = CommonBLL.CreateCommonBLL().GetPersonDetailPostList();

            int numberOfPersons = personDetailsPostList.Length;

            const string filename = "PersonAddressDetails.pdf";

            // Create a new PDF document
            PdfDocument document = new PdfDocument();

            // Create a font
            XFont font = new XFont("Times New Roman", 14, XFontStyle.Regular);


            const PageSize pageSize = PageSize.A4;
            PdfPage page = document.AddPage();
            page.Size = pageSize;
            double columnwidth = (page.Width - 2) / 3;
            double columnHeight = (page.Height - 4);
            double x_firstColumn = 5;
            double x_secondColumn = x_firstColumn + columnwidth + 1;
            double x_thirdColumn = x_secondColumn + columnwidth + 1;

            XGraphics gfx = XGraphics.FromPdfPage(page);
            XTextFormatter tf = new XTextFormatter(gfx);
            XRect rectFirstColumn = new XRect(x_firstColumn, 2, columnwidth, columnHeight);
            XRect rectSecondColumn = new XRect(x_secondColumn, 2, columnwidth, columnHeight);
            XRect rectThirdColumn = new XRect(x_thirdColumn, 2, columnwidth, columnHeight);

            int i = 0;
            int columnNumber = 0;
            int columnLineNumbers = 0;
            int personLineNumbers;
            string personDetailPostString;
            while (i < numberOfPersons)
            {
                personLineNumbers = 0;
                personDetailPostString = "";
                getPersonDetailsAndNumberOfLines(personDetailsPostList[i], out personLineNumbers, out personDetailPostString);
                if (personLineNumbers > 0 && !string.IsNullOrEmpty(personDetailPostString))
                {
                    if (columnLineNumbers + personLineNumbers < 50)
                    {
                        columnLineNumbers += personLineNumbers;
                        text += personDetailPostString;
                    }
                    else
                    {
                        switch (columnNumber)
                        {
                            case 0:
                                tf.DrawString(text, font, XBrushes.Black, rectFirstColumn, XStringFormats.TopLeft);
                                break;
                            case 1:
                                tf.DrawString(text, font, XBrushes.Black, rectSecondColumn, XStringFormats.TopLeft);
                                break;
                            case 2:
                                tf.DrawString(text, font, XBrushes.Black, rectThirdColumn, XStringFormats.TopLeft);
                                break;
                            default:
                                page = document.AddPage();
                                page.Size = pageSize;
                                gfx = XGraphics.FromPdfPage(page);
                                tf = new XTextFormatter(gfx);

                                tf.DrawString(text, font, XBrushes.Black, rectFirstColumn, XStringFormats.TopLeft);
                                columnNumber = 0;
                                break;
                        }
                        columnNumber++;
                        columnLineNumbers = personLineNumbers;
                        text = personDetailPostString;
                    }
                }
                i++;
            }

            // Save the document...
            document.Save(filename);
            // ...and start a viewer.
            System.Diagnostics.Process.Start(filename);
        }

        private static void getPersonDetailsAndNumberOfLines(PersonDetailsPost personDetailsPost, out int lineCount, out string personDetailsPostString)
        {
            lineCount = 0;
            personDetailsPostString = "";

            lineCount++;
            personDetailsPostString += getPersonName(personDetailsPost.Title, 
                personDetailsPost.FirstName, personDetailsPost.LastName) + Environment.NewLine;

            lineCount++;
            personDetailsPostString += personDetailsPost.AddressLine1 + Environment.NewLine;

            if (!string.IsNullOrEmpty(personDetailsPost.AddressLine2))
            {
                lineCount++;
                personDetailsPostString += personDetailsPost.AddressLine2 + Environment.NewLine;
            }

            if (!string.IsNullOrEmpty(personDetailsPost.AddressLine3))
            {
                lineCount++;
                personDetailsPostString += personDetailsPost.AddressLine3 + Environment.NewLine;
            }

            if (!string.IsNullOrEmpty(personDetailsPost.AddressLine4))
            {
                lineCount++;
                personDetailsPostString += personDetailsPost.AddressLine4 + Environment.NewLine;
            }

            if (!string.IsNullOrEmpty(personDetailsPost.Country))
            {
                lineCount++;
                personDetailsPostString += personDetailsPost.Country + Environment.NewLine;
            }

            if (!string.IsNullOrEmpty(personDetailsPost.PostalCode))
            {
                lineCount++;
                personDetailsPostString += "Pin: " + personDetailsPost.PostalCode + Environment.NewLine;
            }

            lineCount += 1;
            personDetailsPostString += Environment.NewLine;
        }

        private static string getPersonName(string title, string firstName, string lastName)
        {
            string personName = "";

            if (!string.IsNullOrEmpty(title))
                personName += title + " ";

            personName += firstName + " " + lastName;

            return personName.ToUpper();
        }
    }
}
